--
-- Author: shunguo.chen
-- Date: 2018-03-09 11:21:11
--
-- ChessPlayer 星空谜棋玩家数据

local UserBaseInfo = require("app.public.dao.UserBaseInfo")
local Skill = require("app.public.dao.Skill")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
	if data then
		self:init(data)
	end

end

-- message StarChessPlayer {
-- 	StarChessPlayerBase baseInfo = 1;
-- 	int32 normalDiceNum = 2;//普通骰子数
-- 	int32 goldeDiceNUm = 3;//黄金骰子数
-- 	int32 totalRemainTime = 4;//剩余时间
-- 	repeated int32 roleIDs = 5;//英雄IDs
-- 	repeated int32 buffIDs = 6;//战斗buffIDs
--	int32 gameBuffID = 8;//游戏buff的ID，只有可能有一个，覆盖逻辑，且只会持续一个回合
-- }
function ClassRef:init(data)
	self.uid = data.baseInfo.uid
	self.pos = data.baseInfo.pos
	self.normalDiceNum = data.normalDiceNum
	self.goldeDiceNUm = data.goldeDiceNUm
	self.totalRemainTime = data.totalRemainTime

	self.roleIDs = {}
	for _,id in ipairs(data.roleIDs) do
		self.roleIDs[#self.roleIDs + 1] = id
	end

	self.buffIDs = {}
	for _,id in ipairs(data.buffIDs) do
		self.buffIDs[#self.buffIDs + 1] = id
	end

	self.gameBuffID = data.gameBuffID

	self._initTime = app:getTime()
end

function ClassRef:setInitTime(time)
	self._initTime = time
end

-- 剩余时间
function ClassRef:getTotalRemainTime()
	local curTime = app:getTime()
	return self.totalRemainTime - math.ceil(curTime - self._initTime)
end

function ClassRef:updateRoles()
	self._roles = nil
end

function ClassRef:getRoles()
	if not self._roles then
		local roles = {}
		for i,v in ipairs(self.roleIDs) do
			roles[#roles + 1] = app:sendMsg("CosplayModel", "getRoleById", v)
		end
		self._roles = roles
	end
	return self._roles
end

-- 技能buff有更新，更新skills
function ClassRef:updateSkills()
	if self._skills then
		local skills = {}
		for _,id in ipairs(self.buffIDs) do
			local found = false
			for _,skill in ipairs(self._skills) do
				if id == skill.id then
					found = true
					skills[#skills + 1] = skill
					break
				end
			end
			if not found then
				local skill = Skill.new()
				skill:read({uid=id})
				skills[#skills + 1] = skill
			end
		end
		self._skills = skills
	end
end

function ClassRef:getSkills()
	if not self._skills then
		local skills = {}
		for i,v in ipairs(self.buffIDs) do
			local skill = Skill.new()
			skill:read({uid=v})
			skills[#skills + 1] = skill
		end
		self._skills = skills
	end
	return self._skills
end

function ClassRef:setBaseInfo(baseInfo)
	self.baseInfo = UserBaseInfo.new(baseInfo)

	-- 此处是角色扮演，服务器不该传过来fashionIDs，客户端先屏蔽掉
	self.baseInfo.fashionIDs = {}
end

function ClassRef:isMe()
	return self.uid == UD.player.uid
end

-- 判断是否失败：骰子用完而没有到达终点
function ClassRef:isFail()
	return (self.normalDiceNum + self.goldeDiceNUm) <= 0 and self.pos < 20
end

-- 判断是否胜利：位置超过终点
function ClassRef:isWin()
	return self.pos >= 20
end

-- 拥有游戏buff
function ClassRef:hasGameBuff()
	return self.gameBuffID > 0
end

-- 游戏buff类型
-- 1下回合投掷点数
-- 2掷出点数后移动
-- 3下回合行动倍率
-- 4下回合暂停
function ClassRef:getGameBuffData(isMyRound)
	if self.gameBuffID > 0 then
		local conf = GD:queryChessBuffConf(self.gameBuffID)
		local msg = isMyRound and L(conf.descSelf) or L(conf.descOpp)
		return tonumber(conf.type), msg
	end
end

function ClassRef:getGameBuffMsg(isMyRound)
	if self.gameBuffID > 0 then
		local conf = GD:queryChessBuffConf(self.gameBuffID)
		local pre = isMyRound and L("lua_code_text_214") or L("lua_code_text_477")
		local msg = table.concat({"\"", pre, " ", L(conf.desc), "\""})
		return msg
	end
end

return ClassRef